using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace PocketApps
{
    public partial class PickFolder : Form
    {

        Stack<string> directories = new Stack<string>();


        public PickFolder()
        {
            InitializeComponent();
        }

        public string SelectedFolder
        {
            get
            {
                return directories.Peek();
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;

        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0)
            {
                diListViewItem selected = (diListViewItem)listView1.Items[listView1.SelectedIndices[0]];
                string currentfolder = selected.BoundDirectoryInfo.FullName;
                directories.Push(currentfolder);
                listfolders(directories.Peek());
            }
        }

        private void listfolders(string parent)
        {
            label1.Invoke(new EventHandler(delegate
            {
                label1.Text = parent;
            }));

            listView1.Invoke(new EventHandler(delegate
            {
                listView1.Items.Clear();
            }));

            DirectoryInfo[] root = new DirectoryInfo(parent).GetDirectories();
            foreach (DirectoryInfo dir in root)
            {

                listView1.Invoke(new EventHandler(delegate
                {
                    listView1.Items.Add(new diListViewItem(dir));
                    listView1.Items[listView1.Items.Count - 1].ImageIndex = 0;
                }));

                //treeView1.Invoke(new EventHandler(delegate
                //{
                //    treeView1.Nodes.Add(new diTreeNode(dir));
                //    treeView1.Nodes[treeView1.Nodes.Count - 1].ImageIndex = 0;
                //}));
            }
        }

        private void PickFolder_Load(object sender, EventArgs e)
        {
            columnHeader1.Width = listView1.Width - 6;
            directories.Push("\\");
            listfolders("\\");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (directories.Count > 1)
                directories.Pop();
            listfolders(directories.Peek());
        }

        private void documentList1_SelectedDirectoryChanged(object sender, EventArgs e)
        {
            Debug.WriteLine("selected directory");
        }

    }

    public class diListViewItem : ListViewItem
    {
        public DirectoryInfo BoundDirectoryInfo;

        public diListViewItem(DirectoryInfo di)
        {
            BoundDirectoryInfo = di;
            this.Text = di.Name;
        }
    }

    public class diTreeNode : TreeNode
    {
        public DirectoryInfo BoundDirectoryInfo;

        public diTreeNode(DirectoryInfo di)
        {
            BoundDirectoryInfo = di;
            this.Text = di.Name;
        }
    }
}